package de.koller.worlders.engine.tiles;

import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.worlders.WSea;
import de.koller.worlders.WorldersRoot;
import de.koller.worlders.engine.entity.IAABB;
import de.koller.worlders.engine.tiles.data.MapData;
import de.koller.worlders.engine.tiles.data.TileHolder;
import de.koller.worlders.engine.tiles.data.TileWrap;
import de.koller.worlders.engine.tiles.light.Light;
import de.koller.worlders.render.IGLRender;
import de.koller.worlders.render.WRendering;

public class TileMap extends WSea implements IGLRender {
	
	private float offsetX, offsetY;
	
	@Child private final MapRendering mapRendering = new MapRendering();
	@Child public final TileHolder tileHolder = new TileHolder();
	@Child private final Light light = new Light();

	@Override
	protected void init() {
		super.init();
		
		wroot.rendering.setTilemapV( this );
	}
	
	@Override
	protected void postChildInit() {
		super.postChildInit();
		
		tileHolder.clearAllTiles();
	}

	public void reloadMap( String mapName, boolean story ) {
		wroot.getWio().save();
		wroot.getUser().setMapName( mapName );
		tileHolder.reloadData( story );
		
		wroot.getEntityContainer().resetSpawn();
		wroot.gamePlayer.getGPO().reset();
		
		tileHolder.clearAllTiles();
	}
	
	public void reloadTile( int tx, int ty ) {
		tileHolder.reloadTile( tx, ty );
	}
	
	@Override
	public void renderGL( int elapsedTime ) {		
		wroot.rendering.worlderV.setTransform();
		mapRendering.renderMap();
		wroot.rendering.worlderV.unsetTransform();
	}
	
	public void renderAfterAll() {
		mapRendering.renderFinalize();
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
			
		if( !wroot.getDebug().isActive() ) {
			IAABB a = wroot.gamePlayer.getGPO().getAABB();
			offsetX = -a.getPosition().getXf();
			offsetX += (WorldersRoot.D_WIDTH/2f/WRendering.SCALE) - a.getSize().getX() * 0.5f;

			offsetY = -a.getPosition().getYf();
			offsetY += (WorldersRoot.D_HEIGHT/2/WRendering.SCALE) - a.getSize().getY() * 0.5f;
		}
		
	}

	public float getOffsetX() {
		return offsetX;
	}

	public float getOffsetY() {
		return offsetY;
	}

	public TileWrap getTileAt( float x, float y ) {
		int tx = (int)Math.floor( x );
		int ty = (int)Math.floor( y );
		
		return tileHolder.getTile( tx, ty );
	}

	public TileWrap searchTile( int tx, int ty ) {
		return tileHolder.getTile( tx, ty );
	}
	
	public void setOffsetX( float offsetX ) {
		this.offsetX = offsetX;
	}

	public void setOffsetY( float offsetY ) {
		this.offsetY = offsetY;
	}

	public MapRendering getMapRendering() {
		return mapRendering;
	}

	public TileHolder getTileHolder() {
		return tileHolder;
	}

	public int getNumberOf( int base, int disp ) {
		int sum = 0;
		int vgl = ColorIntUtil.getIntColor( base, disp, 0, 0 );
		for( int x = 0; x < MapData.MAP_SIZE; x++ )
			for( int y = 0; y < MapData.MAP_SIZE; y++ ) {
				int c = tileHolder.getMapData( x, y );
				int act = ColorIntUtil.getIntColor( ColorIntUtil.getAValue( c ), ColorIntUtil.getRValue( c ), 0, 0 );
				if( vgl == act )
					sum++;
			}
		return sum;
	}
	
	public int getNumberOf( int base ) {
		int sum = 0;
		int vgl = ColorIntUtil.getIntColor( base, 0, 0, 0 );
		for( int x = 0; x < MapData.MAP_SIZE; x++ )
			for( int y = 0; y < MapData.MAP_SIZE; y++ ) {
				int c = tileHolder.getMapData( x, y );
				int act = ColorIntUtil.getIntColor( ColorIntUtil.getAValue( c ), 0, 0, 0 );
				if( vgl == act )
					sum++;
			}
		return sum;
	}

	public Light getLight() {
		return light;
	}
	
}
